package com.gzist.blog_system.controller;

import com.gzist.blog_system.domain.Article;
import com.gzist.blog_system.domain.result.PageBean;
import com.gzist.blog_system.domain.result.Result;
import com.gzist.blog_system.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/article")
@Api(tags = "文章管理模块")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /*
     * @description:
     *  根据id查询单条文章
     * @author: 黄泽建
     **/
    @GetMapping("/getOne")
    @ApiOperation("查询单条文章")
    public Result selectOne(@RequestParam @ApiParam("文章id") Long id) {
        Article article = articleService.getById(id);
        log.info("操作到的数据====>{}", article);
        return Result.success(article);
    }

    /*
     * @description:
     *  发布文章接口
     * @author: 黄泽建
     **/
    @PostMapping("/add")
    @ApiOperation("发布文章")
    public Result add(@RequestBody @ApiParam("新增文章内容") Article article) {
        boolean result = articleService.publish(article);
        log.info("操作到的数据====>{}", result);
        if (!result) return Result.error("发布失败！");
        return Result.success();
    }

    /*
     * @description:
     *  分页查询文章列表
     * @author: 黄泽建
     **/
    @GetMapping
    @ApiOperation("分页查询文章")
    public Result<PageBean<Article>> list(@ApiParam("当前页数") Integer pageNum, @ApiParam("每页条数") Integer pageSize) {
        PageBean<Article> pb = articleService.queryPage(pageNum, pageSize);
        log.info("操作到的数据====>{}", pb);
        return Result.success(pb);
    }

    /*
     * @description:
     *  统计热度前十文章信息
     * @author: 黄泽建
     **/
    @GetMapping("/getHeatArticles")
    @ApiOperation("统计热度前十文章信息")
    public Result getHeatArticles() {
        List<Article> result = articleService.getHeatArticles();
        log.info("操作到的数据====>{}", result);
        return Result.success(result);
    }


    /*
     * @description:
     *  删除文章
     * @author: 黄泽建
     **/
    @DeleteMapping
    @ApiOperation("删除文章")
    public Result deleteArticle(@RequestParam Long id) {
        boolean result = articleService.removeById(id);
        log.info("操作到的数据====>{}", result);
        if (!result) return Result.error("删除失败！");
        return Result.success();
    }

    /*
     * @description:
     *  统计文章数量
     * @author: 黄泽建
     **/
    @GetMapping("/count")
    @ApiOperation("统计文章")
    public Result countArticle() {
        List<Article> list = articleService.list();
        int result = list.size();
        log.info("操作到的数据====>{}", result);
        return Result.success(result);
    }

    /*
     * @description:
     *  通过id更新文章
     * @author: 黄泽建
     **/
    @PostMapping("/update")
    @ApiOperation("更新文章")
    public Result update(@RequestBody @ApiParam("修改文章内容") Article article) {
        boolean result = articleService.updateById(article);
        log.info("更新的数据====>{}", article);
        if (!result) return Result.error("更改失败！");
        return Result.success();
    }
}
